package com.blah.gwtgames.client.common.ui;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Element;

public class SpecialImage extends Image implements
							SourcesSpecialClickEvents {

	SpecialClickListenerCollection specialClickListeners;
	
	public SpecialImage()
	{
		super();
		disableContextMenu(getElement());
	}
	
	public SpecialImage(String url)
	{
		super(url);
		disableContextMenu(getElement());
	}
	
	private native void disableContextMenu(Element elem) /*-{
		elem.oncontextmenu=function(a,b) {return false};
	}-*/;
	
	public void addSpecialClickListener (SpecialClickListener listener)
	{
		if (specialClickListeners == null)
			specialClickListeners = new SpecialClickListenerCollection();
		specialClickListeners.add(listener);
	}
	
	public void removeSpecialClickListener (SpecialClickListener listener)
	{
		if (specialClickListeners != null)
			specialClickListeners.remove(listener);
	}
	
	public void onBrowserEvent(Event event)
	{
		if (DOM.eventGetType(event) == Event.ONMOUSEUP &&
			DOM.eventGetButton(event) == Event.BUTTON_RIGHT)
		{			
			if (specialClickListeners != null)
				specialClickListeners.fireSpecialClick(this);
		}
		else
		{
			super.onBrowserEvent(event);
		}
	}
}
